package com.mrd.util;

import com.mrd.gtimpframe.entity.Dictionary;
import com.mrd.gtimpframe.remote.PolicyRulesParam;

import java.util.Map;

public class CombiMethodUtils {

  /**
   * 规则关系转换为运算符
   * 
   * @author han
   * @param name
   * @return
   */
  public static String getCombi(String name) {
    switch (name) {
      case "EQ":
        return "==";
      case "NE":
        return "!=";
      case "LT":
        return "<";
      case "GT":
        return ">";
    }
    return null;
  }

  /**
   * 单条规则转换
   * 
   * @author han
   * @param rule
   * @return
   */
  public static String ruleCombi(PolicyRulesParam.Rule rule) {
    String compareStr = rule.getMatchType();
    String rtnValue = "";
    if (compareStr.equals("EQ") || compareStr.equals("NE") || compareStr.equals("LT")
        || compareStr.equals("GT")) {
      rtnValue = "$('" + rule.getItemId() + "').val()" + getCombi(compareStr) + rule.getMatchVal();
    } else if ("CN".equals("")) {
      rtnValue = "contain($('" + rule.getItemId() + "').val(),'" + rule.getMatchVal() + "')";
    } else if ("NC".equals("")) {
      rtnValue = "noContain($('" + rule.getItemId() + "').val(),'" + rule.getMatchVal() + "')";
    }
    return rtnValue;
  }

  /**
   * 单条规则转换,并且根据字典翻译
   * 
   * @author han
   * @param rule
   * @param dicMap
   * @return
   */
  public static String ruleCombiDic(PolicyRulesParam.Rule rule, Map<String, Dictionary> dicMap) {
    String compareStr = rule.getMatchType();
    String rtnValue = "";
    String itemIdCode = "";
    if (dicMap != null) {
      if (dicMap.containsKey(rule.getItemId())) {
        itemIdCode = dicMap.get(rule.getItemId()).getDictionaryCode();
      } else {
        itemIdCode = rule.getItemId();
      }
    } else {
      itemIdCode = rule.getItemId();
    }
    itemIdCode = InputRegUtils.replaceInputNameReg(itemIdCode);
    if (compareStr.equals("EQ") || compareStr.equals("NE") || compareStr.equals("LT")
        || compareStr.equals("GT")) {
      rtnValue = "$('" + itemIdCode + "').val()" + getCombi(compareStr) + rule.getMatchVal();
    } else if ("CN".equals(compareStr)) {
      rtnValue = "contain($('" + itemIdCode + "').val(),'" + rule.getMatchVal() + "')";
    } else if ("NC".equals(compareStr)) {
      rtnValue = "noContain($('" + itemIdCode + "').val(),'" + rule.getMatchVal() + "')";
    }
    return rtnValue;
  }

  /**
   * 比较运算符
   * 
   * @author han
   * @return
   */
  public static String compareCombi(String compareStr) {
    switch (compareStr) {
      case "OR":
        return " || ";
      case "AND":
        return " && ";
    }
    return "  ";
  }
}
